﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using SqlSugar;
using WebApplication11.Helper;
using WebApplication11.Models;

namespace WebApplication11.Dao;

public class userDao
{
    public static SqlSugarClient db = dbHelper.CretClient();
    public int Login(string username, string password)
    {
        var res = db.Queryable<user>().Single(it => it.Username == username);
        if (res == null)
            return -1;
        else
            return res.Password == password ? 1 : 0;
    }

    public bool Register(user user)
    {
        user.Password = md5Encrypt(user.Password);
        try
        {
            db.Insertable(user).IgnoreColumns(false, true).ExecuteCommand();
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }
    
    static string md5Encrypt(string str)
    {
        MD5 md5 = MD5.Create();
        byte[] buffer = Encoding.UTF8.GetBytes(str); //将字符串转成字节数组
        byte[] byteArray = md5.ComputeHash(buffer); //调用加密方法
        StringBuilder sb = new StringBuilder();
        foreach (byte b in byteArray) //遍历字节数组
        {
            sb.Append(b.ToString("x2")); //将字节数组转成16进制的字符串。X表示16进制，2表示每个16字符占2位
        }
        return sb.ToString();
    }
    
    public bool destory(String username)
    {
        try
        {
            db.Deleteable<user>().In(it => it.Username, username).ExecuteCommand();
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

    public bool changePassword(String username, String password)
    {
        try
        {
            db.Deleteable<user>().In(it => it.Username, username).ExecuteCommand();
            db.Insertable<user>(new user()
            {
                Username = username,
                Password = md5Encrypt(password),
                DepartmentId = 1
            }).IgnoreColumns(false, true).ExecuteCommand();
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

    public List<forest> getForests(String name)
    {
        var res1 = db.Queryable<user>().ToList();
        var res2 = db.Queryable<department>().ToList();
        var res = new List<forest>();
        foreach (var item1 in res1)
        {
            foreach (var item2 in res2)
            {
                if (item1.Username == name)
                {
                    if (item2.Leader == item1.Username)
                    {
                        res.Add(new forest
                        {
                            Name = item2.Name,
                            Lead = true
                        });
                    }
                    else
                    {
                        res.Add(new forest
                        {
                            Name = item2.Name,
                            Lead = false
                        });
                    }
                }
            }
        }
        return res;
    }
}